package com.example.blog.controller;

import com.example.blog.entity.Article;
import com.example.blog.entity.Comment;
import com.example.blog.entity.User;
import com.example.blog.mapper.ArticleMapper;
import com.example.blog.mapper.CommentMapper;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CommentCtl {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ArticleMapper articleMapper;
    @PostMapping("/comments")
   /* public String addComment(@RequestParam("article_id") Integer article_id,
                             @RequestParam String content,
                             @RequestParam("user_id") Integer user_id,
                             @RequestParam("username") String username,
                             Model model,
                             HttpSession session) {
        Comment comment = new Comment();
        comment.setArticle_id(article_id);
        comment.setUser_id(user_id);
        comment.setCommentTime(new Date());
        comment.setContent(content);
        comment.setUsername(username);
        Integer id=article_id;
        commentMapper.insertComment(comment);

        // 获取文章详情和评论列表
        Article article = commentMapper.selectArticleById(id);
        List<Comment> comments = commentMapper.selectCommentsByArticleId(article_id);
        model.addAttribute("article", article);
        model.addAttribute("comments", comments);

        return "articles"; // 返回文章详情页面
    }*/

    /*@GetMapping("/articles")
    public String getArticleById(@RequestParam int article_id,
                                 Model model) {
        Article article = commentMapper.selectArticleById(article_id);
        List<Comment> comments = commentMapper.selectCommentsByArticleId(article_id);
        model.addAttribute("article", article);
        model.addAttribute("comments", comments);
        return "articles";
    }*/
    public ResponseEntity<?> addComment(@RequestParam("article_id") Integer article_id,
                                        @RequestBody Comment reqComment,
                                        HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("User not logged in");
        }
        String content = reqComment.getContent();
        int result = commentMapper.insertComment(article_id,user.getId(),content, user.getName());
        if (result > 0) {
            Comment comment = new Comment();
            comment.setArticle_id(article_id);
            comment.setUser_id(user.getId());
            comment.setCommentTime(new Date());
            comment.setContent(content);
            comment.setUsername(user.getName());
            //返回单一对象
            Article article = new Article();
            article.setId(article_id);
            article.setComments(articleMapper.selectArticleById(article_id).getComments() + 1);
            articleMapper.updateComments(article);
            //数据库评论数+1
            Map<String, Object> response = new HashMap<>();
            if (comment != null) {
                response.put("success", true);
                response.put("comment", comment);
            } else {
                response.put("success", false);
                response.put("message", "评论提交失败");
            }
            return ResponseEntity.ok(response);
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("评论提交失败");
        }
    }
}